<?php
/**
 */
class ContactsTable extends Doctrine_Table
{
	public function getContacts($user_id)
	{
		$query = new Doctrine_Query();
		$query->select('c.*,u.first_name as first_name,d.photo as photo')
		       ->from('Contacts c')
			   ->leftJoin('c.Users u ON c.contact_id=u.user_id')
			    ->leftJoin('u.UserDetails d ON d.user_id=u.user_id')
			   ->where("c.user_id='".$user_id."'")
			   ->andWhere("c.status !='Inactive'");
			 $result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
		return $result;
	        
	}

	public function getContactsTagging($user_id,$photoId)
	{
		$query = new Doctrine_Query();
		$query->select('c.id,c.contact_id,u.first_name as first_name')
		       ->from('Contacts c')
			   ->leftJoin('c.Users u ON c.contact_id=u.user_id')
			   ->where("c.user_id='".$user_id."'")
			   ->andWhere('u.user_id NOT IN (SELECT p.user_id FROM Tagging p WHERE photo_id = '.$photoId.' GROUP BY p.user_id )')
			   ->groupBy('u.user_id');
		//echo $query->getSqlQuery();exit;
			 $result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
		return $result;
	        
	}
	public function getFriendsContacts($contact_id,$userId)
	{
		$contactList="";
		$frnds_contact_ids = $this->getContacts($userId);
		foreach($frnds_contact_ids as $frnds_contact_ids)
		{
			$contactList.=  $frnds_contact_ids['contact_id'].',';
		}
		$contactList = trim($contactList,',');
		//echo '<pre>';print_r($contactList);exit;
		if($contactList=='')
		{
			$contactList = 0;
		}
		$query = new Doctrine_Query();
		$query->select('c.*,u.first_name')
		       ->from('Contacts c')
			   ->leftJoin('c.Users u ON c.contact_id=u.user_id')
			   ->where("c.user_id='".$contact_id."'")
			   ->andWhere("c.contact_id NOT IN (".$contactList.",".$userId.")")
			   ->andWhere("c.status !='Inactive'");
			   //echo $query;exit;
			 $result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
		return $result;
	        
	}
	public function getMails($memid)
	 {
		$query=new Doctrine_Query();
		    $query->select('DISTINCT(c.contact_email) as email')
		       ->from('Contacts c')
			   ->where("c.user_id='".$memid."'");
			     //echo $query;exit;
			   	$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);
				//echo '<pre>';print_r($result);exit;
				return $result;
	 }
	public function getAllContacts($user_id,$currentPage,$resultsPerPage,$serValue='')
	{
		$query = new Doctrine_Query();
		$query->select('c.*,u.first_name as first_name,u.email as email,d.photo as photo')
		       ->from('Contacts c')
			   ->leftJoin('c.Users u ON c.contact_id=u.user_id')
			    ->leftJoin('u.UserDetails d ON d.user_id=u.user_id')
			   ->where("c.user_id='".$user_id."'")
			   ->andWhere("c.status !='Inactive'");
			   if($serValue!='')
			   {
					$query->andWhere("j.description LIKE '%".$serValue."%'");
			   }
			   if($serValue!='')
			   {
					$query->andWhere("j.description LIKE '%".$serValue."%'");
			   }
			    //echo $query->getSql();exit;
				require_once('../database/models/Pager.php');
				$pager = new Pager($currentPage,$resultsPerPage);
				//echo $query->getSqlQuery();exit;
				$result = $pager->pagination($query);
				//echo '<pre>';print_r($result);exit;
				return $result;
	}
	public function getMyConnectionsArray($userId)
	{
		$query =  new Doctrine_Query;
		$query->select("c.contact_id")
		      ->from(' Contacts c ')
		      ->where("c.status='Active'")
			  ->andWhere('c.user_id='.$userId);
		//echo $query->getSqlQuery(); exit;	  
		$result = $query->execute(array(),Doctrine::HYDRATE_ARRAY);	  
		$connectionsArray = array();
		foreach($result as $res)
		{
			$connectionsArray[] = $res['contact_id'];
		}
		return $connectionsArray;
	}
	public function saveContact($userId,$contactId)
	{
		$contacts = new Contacts();
		$contacts ->user_id = $userId;
		$contacts ->contact_id = $contactId;
        //echo '<pre>';print_r($Expressions);exit;
		$contacts ->save();
		return $contacts->id;
	}
	public function deleteContact($id)
	{
		$query = new Doctrine_Query();
		$query->delete('Contacts c')
			  ->where("c.id='".$id."'");
		//echo $query->getSqlQuery(); exit;
		return $query->execute();	
	}
}